﻿using Peninsula.Utility.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Peninsula.Services
{
    public class SiteFileService : BaseService<DataAccess.POCO.SiteFile>, IServices.ISiteFileService
    {
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isDelPhysical"></param>
        public void DeleteFile(string id, bool isDelPhysical = true)
        {
            string file = Entities.Where(f => f.Id == id).Select(f => f.Path).FirstOrDefault();
            if (isDelPhysical && file.IsNotNullAndNotWhiteSpace())

            {
                try { System.IO.File.Delete(HttpContext.Current.Server.MapPath(file)); }
                catch { }
            }
            Delete(id);
        }
        /// <summary>
        /// 删除多个文件
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="isDelPhysical"></param>

        public void DeleteFiles(string[] ids, bool isDelPhysical = true)
        {
            string[] files = Entities.Where(f => ids.Contains(f.Id)).Select(f => f.Path).ToArray();
            if (isDelPhysical)
            {
                files.ForEach(file =>
                {
                    try { System.IO.File.Delete(HttpContext.Current.Server.MapPath(file)); }
                    catch { }
                });
            }
            for (int i = 0; i < ids.Length; i++)
            {
                Delete(ids[i]);
            }
            //Delete(f => ids.Contains(f.Id));
        }
    }
}
